package com.old.service.Impl;

import com.old.service.UploadFile;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.util.UUID;

@Service
public class UploadFileImpl implements UploadFile {
    @Value("${images.path.local}")
    private String BASE_PATH;
    @Value("${images.path.server}")
    private String IMAGE_PATH;

    public String getBASE_PATH() {
        return BASE_PATH;
    }

    public String uploadFile(File file) throws Exception {
        String path = buildPath(file.getName());// 文件路径
        String folderPath = BASE_PATH + "/" + path.substring(0, path.lastIndexOf("/") + 1);
        File parent = new File(folderPath);
        if (!parent.exists()) {
            parent.mkdirs();
        }

        MultipartFile mf = fileToMultipartFile(file);
        mf.transferTo(new File(BASE_PATH + "/" + path));

        return IMAGE_PATH + "/" + path;
    }

    private MultipartFile fileToMultipartFile(File file) throws Exception {
        FileItem item = new DiskFileItemFactory().createItem("file"
                , MediaType.MULTIPART_FORM_DATA_VALUE
                , true
                , file.getName());
        try (InputStream input = new FileInputStream(file);
             OutputStream os = item.getOutputStream()) {
            // 流转移
            IOUtils.copy(input, os);
        } catch (Exception e) {
            throw new IllegalArgumentException("Invalid file: " + e, e);
        }

        return new CommonsMultipartFile(item);
    }


    public String uploadMultipartFile(MultipartFile file) throws IOException {
        String path = buildPath(file.getOriginalFilename());// 文件路径
        String parentPath = BASE_PATH + "/" + path;
        File parent = new File(parentPath.substring(0, parentPath.lastIndexOf("/")));

        if (!parent.exists()) {
            parent.mkdirs();
        }

        file.transferTo(new File(BASE_PATH + "/" + path));

        return IMAGE_PATH + "/" + path;
    }

    private String buildPath(String originalName) {
//        String prefix = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
        String suffix = originalName.substring(originalName.lastIndexOf("."));
        String filename = UUID.randomUUID().toString();
        String path = filename + suffix;
//        String path =  filename + suffix;
        return path;
    }

    private String getcontentType(String FilenameExtension) {
        if (FilenameExtension.equalsIgnoreCase(".bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase(".gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
                FilenameExtension.equalsIgnoreCase(".jpg") ||
                FilenameExtension.equalsIgnoreCase(".png")) {
            return "image/jpg";
        }
        if (FilenameExtension.equalsIgnoreCase(".html")) {
            return "text/html";
        }
        if (FilenameExtension.equalsIgnoreCase(".txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equalsIgnoreCase(".vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equalsIgnoreCase(".pptx") ||
                FilenameExtension.equalsIgnoreCase(".ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equalsIgnoreCase(".docx") ||
                FilenameExtension.equalsIgnoreCase(".doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equalsIgnoreCase(".xml")) {
            return "text/xml";
        }
        return "image/jpg";
    }
}
